import { ref } from 'vue';
import { IAuditListItem } from '@/apis/common/model/commonModel';
import { GetInventoryOptionListResult } from '@/apis/eci/inventory/model/inventoryModel';
import { getAuditorList } from '@/apis/common/common';
import { getInventoryOptionList } from '@/apis/eci/inventory/inventory';
import { IFillOrganization } from '@/apis/eci/dataManagement/model/configModel';

export const useFillConfig = () => {
  const auditorOptions = ref<IAuditListItem[]>([]);
  const originAuditorOptions = ref<IAuditListItem[]>([]); //y原始数据
  const inventoryOptions = ref<GetInventoryOptionListResult[]>([]);
  const handleFilter = (val: string) => {
    // 过滤方法
    if (val) {
      auditorOptions.value = originAuditorOptions.value.filter(item => {
        return (
          item.realName.toLowerCase().includes(val.toLowerCase()) ||
          item.email.toLowerCase().includes(val.toLowerCase())
        );
      });
    }
  };
  const loadAuditorOptions = (
    isOpen: boolean,
    organizationCode: string,
    row: IFillOrganization
  ) => {
    if (isOpen) {
      // TODO ECI_FILL_TASK_DATA_AUDIT
      getAuditorList(organizationCode, 'ECI_FILL_TASK_DATA_AUDIT').then(res => {
        originAuditorOptions.value = res;
        auditorOptions.value = res;
        if (row.auditorIdTemp) {
          row.auditorId = row.auditorIdTemp;
        }
      });
    }
  };
  const loadInventoryOptions = (isOpen: boolean, organizationCode: string, year: string) => {
    if (isOpen) {
      const params = {
        organizationCode,
        year
      };
      getInventoryOptionList(params).then(res => {
        inventoryOptions.value = res;
      });
    }
  };
  const remoteInventory = (query: string) => {
    if (query) {
      inventoryOptions.value = inventoryOptions.value.filter(item => {
        return item.inventoryName.toLowerCase().includes(query.toLowerCase());
      });
    } else {
      inventoryOptions.value = [];
    }
  };
  const remoteAuditor = (query: string) => {
    if (query) {
      auditorOptions.value = auditorOptions.value.filter(item => {
        return item.realName.toLowerCase().includes(query.toLowerCase());
      });
    } else {
      auditorOptions.value = [];
    }
  };
  const changeAudit = (row: any) => {
    const index = auditorOptions.value.findIndex(n => row.auditorId == n.userId);
    if (index > -1) {
      row.auditorEmail = auditorOptions.value[index].email;
      row.auditorId = auditorOptions.value[index].userId;
      row.auditorIdTemp = auditorOptions.value[index].userId;
      row.auditor = auditorOptions.value[index].realName;
    }
  };
  const changeInventoryList = (row: any) => {
    const index = inventoryOptions.value.findIndex(n => row.inventoryNo == n.inventoryNo);
    if (index > -1) {
      row.inventoryNo = inventoryOptions.value[index].inventoryNo;
      row.inventoryName = inventoryOptions.value[index].inventoryName;
      row.inventoryNoTemp = inventoryOptions.value[index].inventoryNo;
    }
  };
  return {
    auditorOptions,
    inventoryOptions,
    loadAuditorOptions,
    loadInventoryOptions,
    remoteInventory,
    remoteAuditor,
    changeAudit,
    changeInventoryList,
    handleFilter
  };
};
